package com.huopaolan.yohuoshop.Mvp.Presenter;

import com.huopaolan.lib_core.Mvp.Presenter.BasePresenter;
import com.huopaolan.yohuoshop.Bean.CommunityEntity;
import com.huopaolan.yohuoshop.Mvp.Contract.CommunityFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CommunityFragmentPresenter extends BasePresenter<CommunityFragmentContract.CommunityIView, CommunityFragmentContract.CommunityIModel> {
    @Inject
    public CommunityFragmentPresenter(CommunityFragmentContract.CommunityIView communityIView, CommunityFragmentContract.CommunityIModel communityIModel) {
        super(communityIView, communityIModel);
    }

    /**
     * 请求数据
     */
    public void CommunityRequest() {
        JSONObject community = new JSONObject();
        try {
            community.put("categoryid", "1");
            community.put("page", "1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String request = community.toString();
        m.community_request(request).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommunityEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CommunityEntity communityEntity) {
                        buildData(communityEntity);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void buildData(CommunityEntity communityEntity) {
        //banner集合
        List<CommunityEntity.Banner> bannerBeans = buildBanner(communityEntity);
        //brand集合
//        List<CommunityEntity.Brand> brandBeans = buildBrand(communityEntity);
        //values集合
        List<CommunityEntity.Values> valuesBeans = buildValues(communityEntity);
        Map<String, List> map = new HashMap<>();
        map.put("bannerBeans", bannerBeans);
//        map.put("brandBeans", brandBeans);
        map.put("valuesBeans", valuesBeans);
        v.CommunitySuccess(map);
    }

    private List<CommunityEntity.Values> buildValues(CommunityEntity communityEntity) {
        List<CommunityEntity.Values> values = new ArrayList<>();
        for (int i = 0; i < communityEntity.values.size(); i++) {
            if (Integer.parseInt(communityEntity.values.get(i).see_news_id) >= 1 && Integer.parseInt(communityEntity.values.get(i).see_news_id) <= 10) {
                CommunityEntity.Values valueslist = new CommunityEntity.Values();
                valueslist.see_edt_id = communityEntity.values.get(i).see_edt_id;
                valueslist.see_category_id = communityEntity.values.get(i).see_category_id;
                valueslist.see_edt_name = communityEntity.values.get(i).see_edt_name;
                valueslist.see_edt_icon = communityEntity.values.get(i).see_edt_icon;
                valueslist.see_edt_tag = communityEntity.values.get(i).see_edt_tag;
                valueslist.see_edt_values = communityEntity.values.get(i).see_edt_values;
                valueslist.see_news_title = communityEntity.values.get(i).see_news_title;
                valueslist.see_news_img = communityEntity.values.get(i).see_news_img;
                valueslist.see_news_values = communityEntity.values.get(i).see_news_values;
                valueslist.see_news_time = communityEntity.values.get(i).see_news_time;
                valueslist.see_news_num = communityEntity.values.get(i).see_news_num;
                valueslist.see_news_collection = communityEntity.values.get(i).see_news_collection;
                values.add(valueslist);
            }
        }
        return values;
    }

//    private List<CommunityEntity.Brand> buildBrand(CommunityEntity communityEntity) {
//        List<CommunityEntity.Brand> brands = new ArrayList<>();
//        for (int i = 0; i < communityEntity.brand.size(); i++) {
//            if (Integer.parseInt(communityEntity.brand.get(i).brand_id) >= 1 && Integer.parseInt(communityEntity.brand.get(i).brand_id) <= 8) {
//                CommunityEntity.Brand brand = new CommunityEntity.Brand();
//                brand.brand_id = communityEntity.brand.get(i).brand_id;
//                brand.menu_id = communityEntity.brand.get(i).menu_id;
//                brand.brand_name = communityEntity.brand.get(i).brand_name;
//                brand.brand_icon = communityEntity.brand.get(i).brand_icon;
//                brands.add(brand);
//            }
//        }
//        return brands;
//    }

    private List<CommunityEntity.Banner> buildBanner(CommunityEntity communityEntity) {
        List<CommunityEntity.Banner> bannerBeanList = new ArrayList<>();
        for (int i = 0; i < communityEntity.banner.size(); i++) {
            if (Integer.parseInt(communityEntity.banner.get(i).recommend_id) >= 1 && Integer.parseInt(communityEntity.banner.get(i).recommend_id) <= 7) {
                CommunityEntity.Banner banner = new CommunityEntity.Banner();
                banner.recommend_id = communityEntity.banner.get(i).recommend_id;
                banner.recommend_name = communityEntity.banner.get(i).recommend_name;
                banner.recommend_path = communityEntity.banner.get(i).recommend_path;
                banner.recommend_url = communityEntity.banner.get(i).recommend_url;
                bannerBeanList.add(banner);
            }
        }
        return bannerBeanList;
    }
}
